package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.NoticeDao;
import com.example.communist_app_api.db.dao.NoticeRefDao;
import com.example.communist_app_api.db.dao.UserDao;
<<<<<<< HEAD
import com.example.communist_app_api.db.dao.UserOrganizationDao;
import com.example.communist_app_api.db.pojo.NoticeEntity;
import com.example.communist_app_api.db.pojo.NoticeRefEntity;
import com.example.communist_app_api.db.pojo.User;
<<<<<<< HEAD
import com.example.communist_app_api.enums.MessageTypeEnum;
import com.example.communist_app_api.enums.SenderNameEnum;
import com.example.communist_app_api.enums.SenderPhotoEnum;
=======
import com.example.communist_app_api.db.pojoDto.NoticeEntity;
import com.example.communist_app_api.db.pojoDto.NoticeRefEntity;
>>>>>>> 党建uniapp/dev
=======
>>>>>>> parent of aec1d46 (消息模块的搭建和配合其他模块消息的发送和收取)
import com.example.communist_app_api.service.NoticeService;
import com.example.communist_app_api.service.OrganizationService;
import com.example.communist_app_api.service.UserOrganizationService;
import com.example.communist_app_api.vo.uniapp.req.NoticeShowReq;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeDao noticeDao;

    @Autowired
    NoticeRefDao noticeRefDao;

    @Autowired
    private UserOrganizationService userOrganizationService;

    @Autowired
    private UserOrganizationDao userOrganizationDao;

    @Autowired
    private OrganizationService organizationService;



    @Autowired
    UserDao userDao;

    @Override
    public String insertNotice(NoticeEntity entity) {
        return  noticeDao.insert(entity);
    }

    @Override
    public PageInfo<HashMap> searchNoticeByPage(NoticeShowReq req,int userId) {
        Page<HashMap> page = new Page<>(req.getPageNum(),req.getPageSize());
        Integer skip= (req.getPageNum()-1)*req.getPageSize();
        List<HashMap> list = noticeDao.searchNoticeByPage(userId);
        page.setTotal(list.size());
        List<HashMap> returnList = list.stream().skip(skip)
                .limit(req.getPageSize()).collect(Collectors.toList());
        page.addAll(returnList);
        return new PageInfo<>(page);
    }

    @Override
    public List<HashMap> searchMessageByPageUniApp(int userId, long start, int length) {
        List<HashMap> list = noticeDao.searchNoticeByPageUniApp(userId,start,length);
        return list;
    }

    /**
     *
     * @param req 请求参数
     * @param userId    当前登录的用户
     * @return
     */
    @Override
    public PageInfo<HashMap> searchNoticeByWeb(NoticeShowReq req,Integer userId) {
        Page<HashMap> page = new Page<>(req.getPageNum(),req.getPageSize());
        Integer skip= (req.getPageNum()-1)*req.getPageSize();
        List<Integer> userIdList;
        Integer parentId = userOrganizationService.getOrganizationByUserId(userId).getParentId();
        Integer grandparentId = organizationService.getOrganizationByOrganizationId(parentId).getParentId();
        if (grandparentId == 0){
            //true为党总支，查看总支和所有支部的公告
            //查询所有总支成员Id作为userIdList
            userIdList = userOrganizationService.getAllGeneralPartyBranchUserIdyBUserId(userId);
        }else {
            //false为党支部
            //根据organizationId查询本组织的成员，用于查看本组织发布的公告（因为里面有删除操作，这里就不查询党总支的公告操作了）
            userIdList = userOrganizationService.getAllUserIdByUserId(userId);
            userOrganizationDao.getAllUserIdByUserId(userId);
        }
        List<HashMap> list = noticeDao.searchNoticeByUserIdList(userIdList);
        page.setTotal(list.size());
        List<HashMap> returnList = list.stream().skip(skip)
                .limit(req.getPageSize()).collect(Collectors.toList());
        page.addAll(returnList);
        return new PageInfo<>(page);
    }

    @Override
    public HashMap searchNoticeById(String id) {

        return noticeDao.searchNoticeById(id);
    }

    @Override
    public String insertNoticeRef(NoticeRefEntity entity) {
        return noticeRefDao.insert(entity);
    }

    @Override
    public long searchUnreadCount(int userId) {
        return noticeRefDao.searchUnreadCount(userId);
    }

    @Override
    public long searchLastCount(int userId) {
        return noticeRefDao.searchLastCount(userId);
    }

    @Override
    public long updateUnreadCount(String id) {
        return noticeRefDao.updateUnreadCount(id);
    }

    @Override
    public long deleteNoticeRefById(String id) {
        return noticeRefDao.deleteNoticeRefById(id);
    }

    @Override
    public long deleteNoticeById(String id) {
        return noticeDao.deleteNoticeById(id);
    }

    @Override
    public List<String> selectAlreadyReadUser(String id) {
        return noticeRefDao.selectAlreadyReadUser(id);
    }

    @Override
    public long deleteUserNoticeRef(int userId) {
        return noticeRefDao.deleteUserNoticeRef(userId);
    }
}
